package com.miku.blog.service.impl;

import com.miku.blog.domain.*;
import com.miku.blog.domain.dto.*;
import com.miku.blog.domain.pojo.ResponseResult;
import com.miku.blog.domain.pojo.SystemConstance;
import com.miku.blog.domain.vo.AreaVo;
import com.miku.blog.domain.vo.ComponentVo;
import com.miku.blog.eume.HttpStatusEnum;
import com.miku.blog.mapper.ArticleMapper;
import com.miku.blog.mapper.TouristMapper;
import com.miku.blog.mapper.UserLoginInfoMapper;
import com.miku.blog.service.*;
import com.miku.blog.utils.BeanCopyUtils;
import com.miku.blog.utils.DateUtils;
import com.miku.blog.utils.InsertSortUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author shkstart
 * @create ${}YEAR-05-26 17:56
 */
@Service
public class ComponentServiceImpl implements ComponentService {

    @Autowired
    private UserAuthService userAuthService;
    @Autowired
    private ArticleService articleService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private UniqueViewService uniqueViewService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private TagService tagService;
    @Autowired
    private UserLoginInfoService userLoginInfoService;
    @Autowired
    private TouristService touristService;


    @Override
    public ResponseResult getComponentCount() {

        long userCount = userAuthService.count();
        long articleCount = articleService.count();
        long commentCount = commentService.count();
        long uniqueViewCount = 0;
        List<UniqueView> uniqueViewList = uniqueViewService.list();
        for (UniqueView uniqueView : uniqueViewList) {
            uniqueViewCount += uniqueView.getViewsCount();
        }
        ComponentVo componentVo = new ComponentVo();
        componentVo.setUserCount(userCount)
                .setArticleCount(articleCount)
                .setMessageCount(commentCount)
                .setViewsCount(uniqueViewCount)
                .setCategoryDTOList(getComponentCategoryDTOList())
                .setTagDTOList(getComponentTagDTOList())
                .setArticleStatisticsList(getComponentArticleStatisticsDTOList())
                .setUniqueViewDTOList(getComponentUniqueViewDTO())
                .setArticleRankDTOList(getComponentArticleRankDTO());

        return ResponseResult.ok().setData(componentVo);
    }

    //获取分类列表
    public List<CategoryDTO> getComponentCategoryDTOList() {

        List<Article> var1
                = articleService.getArticleListByGroupByCategoryId();
        List<CategoryDTO> categoryDTOList = null;
        if (Objects.nonNull(var1) && var1.size() > 0) {
            categoryDTOList
                    = var1.stream()
                    .map(article -> {
                        List<Article> articles = articleService.getArticleListByCategory(article.getCategoryId(), SystemConstance.ARTICLE_STATUS_DRAFT, SystemConstance.NOT_DELETE);
                        //获取类别对象
                        Category var13 = categoryService.getById(article.getCategoryId());
                        CategoryDTO categoryDTO
                                = new CategoryDTO().setArticleCount(articles.size())
                                .setId(var13.getId())
                                .setCategoryName(var13.getCategoryName());

                        return categoryDTO;
                    }).collect(Collectors.toList());
        }
        return categoryDTOList;
    }

    //获取标签列表
    public List<TagDTO> getComponentTagDTOList() {
        List<Tag> tagList = tagService.list();
        List<TagDTO> tagDTOList = null;
        if (Objects.nonNull(tagList) && tagList.size() > 0) {
            tagDTOList
                    = tagList.stream()
                    .map(tag -> {
                        TagDTO tagDTO = BeanCopyUtils.copyObjectByType(tag, TagDTO.class);
                        return tagDTO;
                    })
                    .collect(Collectors.toList());
        }
        return tagDTOList;
    }

    //获取对应日期下的 文章发布量
    public List<ArticleStatisticsDTO> getComponentArticleStatisticsDTOList() {

        ArticleMapper articleMapper = (ArticleMapper) articleService.getBaseMapper();
        List<Article> var1 = articleMapper.getArticleListByGroupByCreateTime();
        List<ArticleStatisticsDTO> articleStatisticsDTOList = null;
        if (Objects.nonNull(var1) && var1.size() > 0) {
            articleStatisticsDTOList =
                    var1.stream()
                            .map(article -> {
                                List<Article> var2 = articleService.getArticleListByCreateTime(article);

                                String format = DateUtils.format(article.getCreateTime());
                                ArticleStatisticsDTO articleStatisticsDTO = new ArticleStatisticsDTO();
                                articleStatisticsDTO.setDate(format)
                                        .setCount((long) var2.size());

                                return articleStatisticsDTO;
                            }).collect(Collectors.toList());
        }

        return articleStatisticsDTOList;
    }


    //获取近一周的访问量
    public List<UniqueViewDTO> getComponentUniqueViewDTO() {
        List<UniqueView> var1
                = uniqueViewService.getUniqueViewListByGroupByCreateTIme();
        List<UniqueViewDTO> uniqueViewDTOList = null;

        if (Objects.nonNull(var1) && var1.size() > 0) {
            Date date = DateUtils.getDateAtWeekBeforeToday();
            List<UniqueView> var2
                    = var1.stream()
                    .filter(uniqueView -> uniqueView.getCreateTime().after(date))
                    .collect(Collectors.toList());
            if (var2.size() > 0) {
                InsertSortUtils.insertSortByUniqueViewCreateTime(var2);

                uniqueViewDTOList
                        = var2.stream()
                        .map(uniqueView -> {
                            List<UniqueView> var3 = uniqueViewService.getUniqueViewListByCreateTime(uniqueView.getCreateTime());
                            long viewCount = 0;
                            for (UniqueView var12 : var3) {
                                viewCount += var12.getViewsCount();
                            }

                            String format = DateUtils.format(uniqueView.getCreateTime());
                            UniqueViewDTO uniqueViewDTO = new UniqueViewDTO();
                            uniqueViewDTO.setDay(format)
                                    .setViewsCount(viewCount);

                            return uniqueViewDTO;
                        }).collect(Collectors.toList());
            }
        }

        return uniqueViewDTOList;
    }


    //获取前10篇 阅读量最高的文章
    public List<ArticleRankDTO> getComponentArticleRankDTO() {
        List<Article> articleList = articleService.getArticleList();
        List<ArticleRankDTO> articleRankDTOList = null;
        if (Objects.nonNull(articleList) && articleList.size() > 0) {
            articleRankDTOList
                    = articleList.stream()
                    .map(article -> {

                        ArticleRankDTO articleRankDTO = new ArticleRankDTO();
                        articleRankDTO.setArticleTitle(article.getArticleTitle())
                                .setViewsCount(Long.valueOf(article.getViewsCount()));

                        return articleRankDTO;
                    }).collect(Collectors.toList());
        }
        return articleRankDTOList;
    }

    @Override
    public ResponseResult getAreaList(Integer type) {

        List<AreaVo> areaVoList;
        if (SystemConstance.AREA_USER_TYPE.equals(type)) {
            UserLoginInfoMapper userLoginInfoMapper = (UserLoginInfoMapper) userLoginInfoService.getBaseMapper();
            areaVoList = userLoginInfoMapper.getUserLoginInfoByProvincial();
        } else if (SystemConstance.AREA_TOURIST_TYPE.equals(type)) {
            TouristMapper touristMapper = (TouristMapper) touristService.getBaseMapper();
            areaVoList = touristMapper.getTouristListByProvincial(SystemConstance.TOURIST_DEFAULT_NICKNAME);
        }else {
            return new ResponseResult(HttpStatusEnum.PARAM_ERROR);
        }

        Map<String, Long> map = new HashMap<>();
        areaVoList.stream()
                .peek(areaVo -> {
                    String name = areaVo.getName();
                    for (String suffix : SystemConstance.PROVINCIAL_SUFFIX) {
                        if (name.contains(suffix)){
                            String key = name.substring(0, name.indexOf(suffix));
                            if (map.containsKey(key)){
                                map.put(key,map.get(key) + areaVo.getValue());
                            }else {
                                map.put(key, areaVo.getValue());
                            }
                            break;
                        }
                    }
                }).collect(Collectors.toList());

        areaVoList = new ArrayList<>();
        Set<Map.Entry<String, Long>> entrySet = map.entrySet();
        for (Map.Entry<String, Long> entry : entrySet) {
            AreaVo areaVo = new AreaVo();
            areaVo.setName(entry.getKey())
                    .setValue(entry.getValue());
            areaVoList.add(areaVo);
        }

        return ResponseResult.ok().setData(areaVoList);
    }
}
